#!/usr/bin/env python
#encoding=utf8
"""
@描述：用autolog中的suv，pv来测试bdb的读写效果，为以后的应用做铺垫
@作者：范红星
@日期：2014年12月1日 星期一
"""
import bsddb3
import sys
import time

env_path = "/cygdrive/d/project/DataBase/berkeleyDB/db/"
db_path = env_path + "bdb.db"

dbenv = bsddb3.db.DBEnv()
dbenv.open(env_path, bsddb3.db.DB_CREATE |bsddb3.db.DB_INIT_CDB| bsddb3.db.DB_INIT_MPOOL)
db = bsddb3.db.DB(dbenv)
db.open(db_path, bsddb3.db.DB_BTREE, bsddb3.db.DB_CREATE, 0660)

def writeToBDB(suv_path):
	try:
		num = 0
		
		print(time.strftime('%Y-%m-%d %H:%M:%S'))
		
		hdl = open(suv_path,"r")
		for line in hdl:
			num = num + 1
			if(num % 10000 == 0):
				db.sync()
				#print(time.strftime('%Y-%m-%d %H:%M:%S')),
				print("%s\t%d" %("Num is:",num))
			line = line.strip()
			cols = line.split("\t")
			suv = cols[0]
			pv = cols[1]
			db[suv] = pv
	except Exception,e:
		print(e)

if __name__ == "__main__":
	suv_path = "/cygdrive/d/project/DataBase/berkeleyDB/"
	suv_path = suv_path + sys.argv[1]
	writeToBDB(suv_path)

	print(time.strftime('%Y-%m-%d %H:%M:%S'))
	print("%s\t%s" %("1404201654438442", db.get("1404201654438442")))

	db.close()
	dbenv.close()
